package com.neusoft.controller;


import com.neusoft.pojo.Dept;
import com.neusoft.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供 restFul 服务
@RestController
public class DeptController {

    @Autowired
    DeptService deptService;

    //获取一些配置信息
    @Autowired
    private DiscoveryClient client;


    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody Dept dept){

        return deptService.addDept(dept);
    }

    @GetMapping("/dept/queryById/{id}")
    public Dept queryById(@PathVariable("id") long id){

        return deptService.queryById(id);
    }

    @GetMapping("/dept/queryAll")
    public List<Dept> queryAll(){

        return deptService.queryAll();
    }


    /*获取注册进来微服务的信息*/
    @RequestMapping("/dept/discovery")
    public Object discovery(){
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=======>>>>:"+services);

        //得到一个具体的微服务信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLUDE-PROVIDER-DEPT");

        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+"\n"+
                    instance.getPort()+"\n"+
                    instance.getUri()+"\n"+
                    instance.getServiceId());
        }
        return this.client;

    }
}
